using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form;
using Volo.Abp.ObjectExtending;
using Volo.Abp.Validation;

namespace YunxSoft.Abp.LanguageManagement.Web.Pages.LanguageManagement.Languages;

public class EditModalModel : LanguageManagementPageModel
{
    [BindProperty]
    public LanguageModel Language { get; set; }

    protected ILanguageAppService LanguageAppService;

    public EditModalModel(ILanguageAppService languageAppService)
    {
        LanguageAppService = languageAppService;
    }
    
    public virtual async Task<IActionResult> OnGetAsync(Guid id)
    {
        Language = ObjectMapper.Map<LanguageDto, LanguageModel>(await LanguageAppService.GetAsync(id));

        return Page();
    }

    public virtual async Task<IActionResult> OnPostAsync()
    {
        ValidateModel();

        var input = ObjectMapper.Map<LanguageModel, UpdateLanguageDto>(Language);
        await LanguageAppService.UpdateAsync(Language.Id, input);

        return NoContent();
    }

    public class LanguageModel : ExtensibleObject
    {
        [HiddenInput]
        public Guid Id { get; set; }
        
        [DynamicStringLength(typeof(LanguageConsts), nameof(LanguageConsts.MaxDisplayNameLength))]
        public string DisplayName { get; set; }
        
        public bool IsEnabled { get; set; }

        public string FlagIcon { get; set; } = "";
    }
}